﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;

namespace TelerikWrappedControl
{
    public partial class CodaProduzioneTelerikGrid : UserControl
    {
        public CodaProduzioneTelerikGrid()
        {
            InitializeComponent();
        }

        public IEnumerable<object> SelectedItems
        {
            get { return radGrid.SelectedItems; }
        }
        public void ClearFilter()
        {
            if (System.Windows.Deployment.Current.Dispatcher.CheckAccess())
            {
                radGrid.FilterDescriptors.Clear();
                textBoxFilterValue.Text = string.Empty;

            }
            else
            {
                var e = new ManualResetEvent(false);
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    radGrid.FilterDescriptors.Clear();
                    textBoxFilterValue.Text = string.Empty;

                    e.Set();
                });
                e.WaitOne();
            }
        }

        private void Filter()
        {
            radGrid.FilterDescriptors.Clear();
            textBoxFilterValue.Text = string.Empty;

            var ageColumn = this.radGrid.Columns["StatoAvanzamento"];

            var ageColumnFilter = new ColumnFilterDescriptor((IDataFieldDescriptor)ageColumn);

            var statiDisponibili = radGrid.GetDistinctValues((IDataFieldDescriptor)ageColumn, true);
            var statiInCoda = new List<int> { 0, 1, 2, 6 };

            foreach (var i in statiDisponibili)
            {
                if (i == null) continue;

                var ii = i as Int16?;

                if (!ii.HasValue) continue;

                if (statiInCoda.Contains(ii.Value))
                {
                    ageColumnFilter.DistinctFilter.DistinctValues.Add(ii.Value);
                }
            }

            ColumnSortDescriptor csd = new ColumnSortDescriptor()
            {
                Column = this.radGrid.Columns["DataConsegna"],
                SortDirection = System.ComponentModel.ListSortDirection.Ascending
            };

            this.radGrid.FilterDescriptors.Add(ageColumnFilter);
            this.radGrid.SortDescriptors.Add(csd);

        }

        public void InCoda()
        {
            if (System.Windows.Deployment.Current.Dispatcher.CheckAccess())
            {
                Filter();
            }
            else
            {
                var e = new ManualResetEvent(false);
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                                             {
                                                                                 Filter();

                                                                                 e.Set();
                                                                             });
                e.WaitOne();
            }
        }

        private CustomFilterDescriptor customFilterDescriptor;

        public CustomFilterDescriptor CustomFilterDescriptor
        {
            get
            {
                if (this.customFilterDescriptor == null)
                {
                    this.customFilterDescriptor = new CustomFilterDescriptor(this.radGrid.Columns.OfType<GridViewDataColumn>());
                    this.radGrid.FilterDescriptors.Add(this.customFilterDescriptor);
                }
                return this.customFilterDescriptor;
            }
        }


        /// <summary>
        /// Quando pulisco il filtro , vengono rimossi tutti i filtri.
        /// Quando 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxFilterValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            //var cFilter = radGrid.FilterDescriptors.OfType<CustomFilterDescriptor>().Count();
            //var filter = radGrid.FilterDescriptors.Count;

            for (int i = 0; i < radGrid.FilterDescriptors.Count; i++)
            {
                if (!(radGrid.FilterDescriptors[i] is CustomFilterDescriptor))
                {
                    radGrid.FilterDescriptors.RemoveAt(i);
                }
            }

            this.CustomFilterDescriptor.FilterValue = this.textBoxFilterValue.Text;

            if(!radGrid.FilterDescriptors.Contains(customFilterDescriptor))
                radGrid.FilterDescriptors.Add(customFilterDescriptor);

            this.textBoxFilterValue.Focus();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            textBoxFilterValue.Text = string.Empty;
        }
    }
}
